﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FastShip88.Admin
{
    public partial class AdmParcelTransfer : System.Web.UI.Page
    {
        DBAccess.UsersDataContext db;
        decimal totalAmount;

        public string StartChoice
        {
            get { return ViewState["StartChoice"].ToString(); }
            set { ViewState["StartChoice"] = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DBAccess.UsersDataContext();

            lblError.Visible = false;
            lblSuccess.Visible = false;

            if (!IsPostBack)
                init_pending_req_grid();


        }


        protected void gvPendingReq_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPendingReq.PageIndex = e.NewPageIndex;
            init_pending_req_grid();
        }

        private void init_pending_req_grid()
        {

            var transReqs = from t in db.TransferReqs
                            from b in db.UserBills
                            from addr in db.UserAddresses
                            from u in db.UserInfos
                            where  t.IsCancelled == false && t.IsUserConfirmed == false && t.TransferDate == null &&
                                    t.UserBillId == b.Id && t.UserId == u.Id && t.TransferAddr == addr.id
                            select new { t.Id, t.Description, t.ReqDate, t.Memo, IsPaid = b.IsCleared, UserName = u.LastName + u.FirstName, addr.receiptor, TransferAddr = addr.Address };

            //TODO: Remove the @ in address
            //foreach (var t in transReqs)
            //{
            //    t.TransferAddr = t.TransferAddr.Replace("@", "");
            //}

            gvPendingReq.DataSourceID = null;
            gvPendingReq.DataSource = transReqs;
            gvPendingReq.DataBind();

            divParcelsInReq.Visible = false;
        }

        protected void gvPendingReq_SelectedIndexChanged(object sender, EventArgs e)
        {
            divParcelsInReq.Visible = true;
            gvParcelsInReq.DataBind();
            
        }

        protected void btnTransfer_Click(object sender, EventArgs e)
        {
            if (gvPendingReq.SelectedIndex == -1)
            {
                lblError.Text = "请先选中一条中转记录。";
                lblError.Visible = true;
                return;
            }

            int operatorId = Utility.GetUserId(this);
            int transReqId = SafeConvert.ToInt(gvPendingReq.SelectedDataKey.Value);

            var q_trans = (from q in db.TransferReqs
                           where q.Id == transReqId
                           select q).First();

            q_trans.TransferDate = DateTime.Now;
            q_trans.Memo = txtMemo.Text.Trim();
            
            try{
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
                lblError.Visible = true;

                return;
            }

            lblSuccess.Text = "已成功中转。";
            lblSuccess.Visible = true;

            Utility.SendEmail(Utility.EmailTypes.ParcelTransferred, q_trans.UserId.Value , q_trans.Id.ToString());

            init_pending_req_grid();

        }

        protected void gvParcelsInReq_RowEditing(object sender, GridViewEditEventArgs e)
        {
            
        }

        protected void gvParcelsInReq_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

        protected void gvParcelsInReq_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            
        }


    }
}
